<html>

<head>
<title>Depth Testing a Lighting</title>
</head>

<body stylesrc="http://buggy/codecolony/home.htm" bgcolor="#000000" text="#FF0000" link="#ABABAB" vlink="#D6B001" alink="#FFFF00">
<font SIZE="2">

<p></font><font size="6"><strong>Depth Testing and Lighting</strong></font><font size="3"></p>

<p></font><font size="4"><strong>You'll learn now:</strong></p>

<blockquote>
  <ul>
    <li></font><font size="3">How you turn on depth testing</li>
    <li>How you enable lighting</li>
    <li>How you specify the light sources</li>
    <li>How you specify material properties<br>
    </li>
  </ul>
</blockquote>

<p></font><font size="4"><strong>How can I turn on depth testing?</strong></p>
</font>

<p><font size="4"><small>Depth testing is used to remove hidden surfaces. We did not yet
need it, because we always rendered wired objects. Lighting makes much more sense, when
objects are filled and wired model are everything else but certainly not realistic.</small></font></p>

<p><font size="4"><small>Depth testing is difficult easy to use: Pass one more constant to
<em>glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH);</em></small></font></p>

<p><font size="4"><small>Now a depth buffer is created on which the depth information is
stored for each pixel. </small></font></p>

<p><font size="4"><small>The next thing is to enble it: <em>glEnable(GL_DEPTH_TEST); </em>Now
you can draw your objects as normal.</small></font></p>

<p><font size="4"><small>Depth buffering is sometimes also called z-buffering.</small></font></p>
<font size="3">

<p></font><font size="4"><strong>How can I enable lighting?</strong></p>
</font>

<p><font size="3">Just call <em>glEnable(GL_LIGHTING);</em></font></p>
<font size="3">

<p>Then you have to enable each of the lights you use, for example <em>glEnable(GL_LIGHT0);</em></font></p>
<font size="3">

<p></font><font size="4"><strong>How can I specify light sources?</strong></p>
</font>

<p><font size="3">Lighting is a very complex topic and you can simulate every possible
light with OpenGL. I only want to explain the basics here. In the DisplayList-tutorial,
two sided lighting is explained.</font></p>

<p>Perhaps I'll write another tutorial that goes deeper into lighting.</p>

<p>To specify light properties, you have to use a <em>glLight*()</em> command, where *
stands for f or i plus, if you want to specify the value as a vector, &quot;v&quot;.</p>

<p>To pass the position to OpenGL you first define a four-dimensional vector (x,y,z and
w). The x, y and z values are divided by w, that means that if you set &quot;w&quot; to
0.0, the position rises infinitely far away. An example for this is the sun. If you want
to use a so called &quot;positional&quot; light (i.e. w is nonzero) you should set w to
1.0.</p>

<p><em>static GLfloat LightPos[] = {0.5, 0.5, 1.0, 1.0};</em></p>

<p>Then you call </p>

<p><em>glLightfv(GL_LIGHT0, GL_POSITION, LightPos);</em></p>

<p>Of course you can use other values for GL_LIGHT0.&nbsp; Note that for keeping the light
where you want it, you should call this command after each <em>Camera.Render()</em></p>

<p>Another important value is the color. You can define different colors for different
properties. Those different properties can be described when you imagine a metal sphere,
once lit by flashlight, once with a candle. Even if there is no other light source in the
room, the objects are visible from each side. This is caused by reflections from the
walls. I could imagine that a candle makes the sides of an object, that look away from the
light, brighter than a flashlight. Perhaps that's wrong, I don't know, ask a physics
expert or check it out! This value is called &quot;ambient&quot;. Another value (diffuse),
is the property, how much the polygons are lit when they don't face the light directly. A
third parameter is called &quot;specular&quot;. It defines, how much of the light is
reflected. Here the value of the flashlite is probably higher than for the candle. You
should also use different colors: The candle has a light that goes to yellow, a flashlight
more white.</p>

<p>Have a look at the source of the tutorial to see the use of that.</p>

<p><font size="4"><strong>How can I specify material properties?</strong></font></p>

<p><font size="4"><small>Material properties work similar to light properties. Of course
they don't have a position, but also ambient, diffuse and specular properties. Especially
the specular value differs from material to material: A metal object is much more shiny
than a wooden one.</small></font></p>

<p><font size="4"><small>The command to specify the properties is <em>glMaterial*()</em>.
&nbsp; The first argument is either, GL_FRONT, GL_BACK or GL_FRONT_AND_BACK. It is
sometimes senseless to make the lighting calculations for the back side. But how does
OpenGL know, which side is fron and which is back? This is done by the orientation of the
vertices. By a call to <em>glFrontFace()</em> you can define, whether the front facing
polygons should be the ones which are clockwise oriented (GL_CW) or counterclockwise
(GL_CCW). If you pass GL_FRONT or GL_BACK to <em>glMaterial*()</em>&nbsp; you should note,
that your models must be in the correct format, otherwise you'll get wrong results. Here
an example how you could set a material's property:</small></font></p>

<p><em>static GLfloat MatAmb[]<small> = {</small> 0.4, 0.4, 0.4, 1.0};</em></p>

<p><em>static GLfloat MatDiff[] =<small> {</small> 0.8, 0.6, 0.6, 1.0};</em></p>

<p><em>glMaterialfv(GL_FRONT, GL_AMBIENT, MatAmb);<br>
glMaterialfv(GL_FRONT, GL_DIFFUSE, MatDiff);</em></p>

<p><font size="3"><a href="../opengl.htm#depthlight">Here</a> you can see the results of
using this tutorial.</font></p>

<p><a href="../opengl.htm"><font size="3">Back</font></a></p>

<p><big>**************************************************************************</big></p>

<p><big>Any comments? Conact me!</big></p>

<p><big><a href="mailto:philipp.crocoll@web.de">philipp.crocoll@web.de</a></big></p>

<p><big><a href="http://www.codecolony.de">www.codecolony.de</a></big></p>
</body>
</html>
